<?php

namespace amap;

use fast\Http;
use think\Exception;

/**
 * 高德地图
 **/
class restapi
{

    protected $url = 'https://restapi.amap.com/';
    protected $api = [
        'direction_driving' => 'v5/direction/driving',
    ];
    protected $key;

    public function __construct($key = '')
    {
        $config = config('amap');
        $this->key = $key ? $key : $config;
    }

    /**
     * 驾车路线规划
     * @param string $origin 起点经纬度
     * @param string $destination 目的地
     * @param integer $strategy 驾车算路策略
     * @param string $waypoints 途经点
     * @param integer $ferry 是否使用轮渡
     * @return array
     */
    public function direction_driving($origin, $destination, $strategy, $waypoints, $ferry = 1)
    {
        $api = $this->api[__FUNCTION__];
        $url = $this->url . $api;
        $params = [
            "key" => $this->key,
            "origin" => $origin, // 经度在前，纬度在后，经度和纬度用","分割，经纬度小数点后不得超过6位。
            "destination" => $destination, // 经度在前，纬度在后，经度和纬度用","分割，经纬度小数点后不得超过6位。
            "strategy" => $strategy, // 授权类型，此处只需填写 authorization_code
            "waypoints" => $waypoints, // 途径点坐标串，默认支持1个有序途径点。多个途径点坐标按顺序以英文分号;分隔。最大支持16个途经点。
            "ferry" => $ferry, // 0:使用渡轮1:不使用渡轮
            "show_fields" => "navi,tmcs,cost,polyline", // 返回结果控制
        ];
        $result = Http::get($url, $params);
        user_log('restapi', $result);
        $result = json_decode($result, true);
        if ($result['status'] == 0 || $result['infocode'] != 10000) {
            throw new Exception('【地图】code:' . $result['infocode'] . ',' . $result['info']);
        }
        return $result;
    }

}
